//
// Created by yangchao on 2022/7/13.
// 26. 删除有序数组中的重复项: https://leetcode.cn/problems/remove-duplicates-from-sorted-array/
//

#include <vector>
#include <iostream>

using namespace std;

class RemoveDuplicates {
public:
    int removeDuplicates(vector<int>& nums) {
        if (nums.empty()) return 0;
        int slowIndex = 0;  //指向数组最后一个元素的位置
        for (int fastIndex = 0; fastIndex < nums.size(); ++fastIndex) {
            if (nums[fastIndex] != nums[slowIndex]) {
                nums[++slowIndex] = nums[fastIndex];
            }
        }
        return slowIndex+1;
    }

    int removeDuplicates2(vector<int>& nums) {
        int len = nums.size();
        if(len == 0) return 0;
        int fast = 1, slow = 1;
        while(fast < len) {
            if(nums[fast] != nums[fast-1]) {
                nums[slow] = nums[fast];
                slow++;
            }
            fast++;
        }
        return slow;
    }
};

int main() {
    vector<int> nums;
    nums.push_back(0);
    nums.push_back(0);
    nums.push_back(1);
    nums.push_back(1);
    nums.push_back(1);
    nums.push_back(2);
    nums.push_back(2);
    nums.push_back(3);
    nums.push_back(3);
    nums.push_back(4);

    RemoveDuplicates rd;
    int res = rd.removeDuplicates(nums);

    cout << res << endl;


};

/*输入：nums = [0,0,1,1,1,2,2,3,3,4]
输出：5, nums = [0,1,2,3,4]*/

